import { Plugin, TFile } from 'obsidian';
//import * as path from 'path';
//import { Utils } from 'utils';
import { LinksHandler } from './LinksHandler';
import { AbitUsefulSettingTab } from './AbitUsefulSettingTab';

// Remember to rename these classes and interfaces!

export interface AbitUsefulSettings {
	//文件归集目录
	exportNotesFolder: string;
	deleteOriginNote: boolean;
	allNoteAttachmentResultFile: string;

	allInlinkNotesResultFile: string;
}

const DEFAULT_SETTINGS: AbitUsefulSettings = {
	//默认的归集目录
	exportNotesFolder: '导出目录',
	allNoteAttachmentResultFile: 'all-note-attachment-check-result.md',
	allInlinkNotesResultFile: 'all-file-inlink-notes.md',
	deleteOriginNote: false,
}

export default class AbitUseful extends Plugin {
	settings: AbitUsefulSettings;
	lh: LinksHandler;
	async onload() {
		await this.loadSettings();
		this.lh = new LinksHandler(this.app, this.settings);
		this.addSettingTab(new AbitUsefulSettingTab(this.app, this));


		this.registerEvent(
			this.app.workspace.on("file-menu", (menu, file, source) => {
				if (file instanceof TFile && file.extension === "md") {
					menu.addItem((item) => {
						item
							.setTitle("导出(移动)笔记及附件 👈")
							.setIcon("document")
							.onClick(async () => {
								await this.lh.exportNote(file);
							});
					});
					menu.addItem((item) => {
						item
							.setTitle("整理笔记附件 👈")
							.setIcon("document")
							.onClick(async () => {
								await this.lh.collectNote(file);
							});
					});
				}

				file instanceof TFile && menu.addItem((item) => {
					item
						.setTitle("显示文件状态 👈")
						.setIcon("document")
						.onClick(async () => {
							await this.lh.showNoteStatus(file);
						});
				});
			})

		);

		this.registerEvent(
			this.app.workspace.on("editor-menu", (menu, editor) => {
				menu.addItem((item) => {
					item
						.setTitle("换行符前加入两个空格")
						.setIcon("document")
						.onClick(() => {
							this.lh.addTwoSpaceBeforeEnter(editor);
						});
				});
			})
		)

		this.addCommand({
			id: "check-all-note-attachments", name: "检查所有笔记附件一致性", callback: async () => {
				console.log("check-all-note-attachments");
				await this.lh.checkAllNoteAttachments();
			}
		});

		this.addCommand({
			id: "check-all-file-inLinkNotes", name: "检查所有文件入链文件", callback: async () => {
				await this.lh.checkAllFileInLinkNotes();
			}
		});
	}

	onunload() {

	}

	async loadSettings() {
		this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
	}

	async saveSettings() {
		await this.saveData(this.settings);
	}

}

